// pages/writeEXP/writeEXP.js
var api = require('../../config/api.js');
var util = require('../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    studentTypes: [
      { value: '1', name: '国内', checked: 'true' },
      { value: '2', name: '海外(含港澳台)' },
    ],
    degree: [
      { value: '1', name: '本科', checked: 'true' },
      { value: '2', name: '硕士', },
      { value: '3', name: 'MBA', },
      { value: '4', name: '硕博连读', },
      { value: '5', name: '博士', },
      { value: '6', name: '博士后', },
      { value: '7', name: '其他', },
    ],
    year:[
      { value: '2022', name: '2022', checked: 'true' },
      { value: '2021', name:'2021', },
      { value: '2020', name: '2020'},
      { value: '2019', name: '2019'},
      { value: '2018', name: '2018'},
      { value: '2017', name: '2017'},
      { value: '2016', name: '2016'},
      { value: '其他', name: '其他'},
    ],
    season:[
      { value: '秋季', name: '秋季', checked: 'true' },
      { value: '春季', name: '春季',},
      { value: '夏季', name: '夏季',},
    ],
    result:[
      { value: '1', name: '已录取',checked: 'true' },
      { value: '2', name: '初试通过',},
      { value: '3', name: 'AD小奖', },
      { value: '4', name: '全奖',},
      { value: '5', name: '被拒',},
      { value: '6', name: 'waiting list',},
    ],
    checkboxChecked: false,
    studentType: "1",
    offerType: "1",
    schoolName: "",
    specialName: "",
    enrollmentYear: "2021",
    enrollmentSemester: "秋季",
    admissionResult:"1",
    noticeTime:"",
    isAnonymous: "",
    userId: "",
    userName: "",
    date: '请选择通知时间',
    time: '12:01',
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  isHideNameChange: function (e) {
    let isHideName = this.data.checkboxChecked;
    isHideName = !isHideName;
    console.log(isHideName)
    this.setData({
      checkboxChecked: isHideName,
      isAnonymous: isHideName ? 1 : 0
    })
  },
  studentTypeRadioChange(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    const studentTypes = this.data.studentTypes;
    for (let i = 0, len = studentTypes.length; i < len; ++i) {
      studentTypes[i].checked = studentTypes[i].value === e.detail.value
    }
    this.setData({
      studentTypes: studentTypes,
      studentType: e.detail.value
    })
  },
  degreeTypeRadioChange(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    const degree = this.data.degree;
    for (let i = 0, len = degree.length; i < len; ++i) {
      degree[i].checked = degree[i].value === e.detail.value
    }
    this.setData({
      degree: degree,
      offerType: e.detail.value
    })
  },
  // 入学年份
  inSchool(e){
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    const year = this.data.year;
    for (let i = 0, len = year.length; i < len; ++i) {
      year[i].checked = year[i].value === e.detail.value
    }
    this.setData({
      year: year,
      enrollmentYear: e.detail.value
    })
  },
  // 入学学期
  inSeason(e){
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    const season = this.data.season;
    for (let i = 0, len = season.length; i < len; ++i) {
      season[i].checked = season[i].value === e.detail.value
    }
    this.setData({
      season: season,
      enrollmentSemester: e.detail.value
    })
  },
  // 录取结果
  offserResult(e){
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    const result = this.data.result;
    for (let i = 0, len = result.length; i < len; ++i) {
      result[i].checked = result[i].value === e.detail.value
    }
    this.setData({
      result: result,
      admissionResult: e.detail.value
    })
  },
  //发布offer
  offerSubmit: function (e) {
    var data = this.data;
    if(!data.studentType){
      wx.showToast({
        title: '请选择考生类型',
        icon: 'none',
        duration: 1500
      })
      return ;
    }
    if(!data.offerType){
      wx.showToast({
        title: '请选择offer类型',
        icon: 'none',
        duration: 1500
      })
      return ;
    }
    if(!data.schoolName){
      wx.showToast({
        title: '请填写院校名称',
        icon: 'none',
        duration: 1500
      })
      return ;
    }
    if(!data.specialName){
      wx.showToast({
        title: '请填录取专业',
        icon: 'none',
        duration: 1500
      })
      return ;
    }
    if(!data.enrollmentYear){
      wx.showToast({
        title: '请选择入学年份',
        icon: 'none',
        duration: 1500
      })
      return ;
    }
    if(!data.enrollmentSemester){
      wx.showToast({
        title: '请选择入学学期',
        icon: 'none',
        duration: 1500
      })
      return ;
    }
    if(!data.admissionResult){
      wx.showToast({
        title: '请选择录取结果',
        icon: 'none',
        duration: 1500
      })
      return ;
    }
    if(!data.date || data.date === "请选择通知时间"){
      wx.showToast({
        title: '请选择通知时间',
        icon: 'none',
        duration: 1500
      })
      return ;
    }
    var offer = data;
    var userInfo = wx.getStorageSync('userInfo') //获取用户
    offer.userId = userInfo.id
    offer.userName = userInfo.nickName
    util.request(api.OfferAdd, offer, 'POST', true).then(function (res) {
      if (res.code === 200) {
        wx.setStorageSync('sendOfferSuccess', true);
        wx.switchTab({
          url: '../../pages/index/index',
          success : function(){
            wx.showToast({
              title: '恭喜您！提交成功啦！现在您可以去【个人中心】-【创作中心】查看您的发布！',
              icon: 'none',
              duration: 2200
            });
          }
        })
      }
    })
  },
  offerSave: function(e){
    let data = this.data;
    if(!data.studentType){
      wx.showToast({
        title: '请选择考生类型',
        icon: 'none',
        duration: 1500
      })
      return ;
    }
    if(!data.offerType){
      wx.showToast({
        title: '请选择offer类型',
        icon: 'none',
        duration: 1500
      })
      return ;
    }
    if(!data.schoolName){
      wx.showToast({
        title: '请填写院校名称',
        icon: 'none',
        duration: 1500
      })
      return ;
    }
    if(!data.specialName){
      wx.showToast({
        title: '请填录取专业',
        icon: 'none',
        duration: 1500
      })
      return ;
    }
    if(!data.enrollmentYear){
      wx.showToast({
        title: '请选择入学年份',
        icon: 'none',
        duration: 1500
      })
      return ;
    }
    if(!data.enrollmentSemester){
      wx.showToast({
        title: '请选择入学学期',
        icon: 'none',
        duration: 1500
      })
      return ;
    }
    if(!data.admissionResult){
      wx.showToast({
        title: '请选择录取结果',
        icon: 'none',
        duration: 1500
      })
      return ;
    }
    if(!data.date || data.date === "请选择通知时间"){
      wx.showToast({
        title: '请选择通知时间',
        icon: 'none',
        duration: 1500
      })
      return ;
    }
    var offer = {
      checkboxChecked: data.checkboxChecked,
      studentType: data.studentType,
      offerType: data.offerType,
      schoolName: data.schoolName,
      specialName: data.specialName,
      enrollmentYear: data.enrollmentYear,
      enrollmentSemester: data.enrollmentSemester,
      admissionResult: data.admissionResult,
      noticeTime: data.date,
      isAnonymous: data.isAnonymous
    }
    var userInfo = wx.getStorageSync('userInfo') //获取用户
    offer.userId = userInfo.id
    offer.userName = userInfo.nickName
    wx.setStorageSync('offer', offer);
    wx.switchTab({
      url: '../../pages/index/index',
      success: function() {
        console.log("send OK");
      },
      fail: function(e) {
        console.log(e);
        console.log("send fail");
      }

    })
  },
  setValue: function (e) {
    this.setData({
      [e.currentTarget.dataset.name]: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var offer = options.offer;
    if(offer){
      var offerObj = JSON.parse(offer);
      console.log(offerObj);
      if(offerObj.isAnonymous){
        console.log(offerObj.checkboxChecked);
        this.setData({
          isAnonymous: true,
          checkboxChecked: true
        })
      }
      this.setData({
        studentType: offerObj.studentType,
        offerType: offerObj.offerType,
        schoolName: offerObj.schoolName,
        specialName: offerObj.specialName,
        enrollmentYear: offerObj.enrollmentYear,
        enrollmentSemester: offerObj.enrollmentSemester,
        admissionResult:offerObj.admissionResult,
        noticeTime:offerObj.noticeTime,
        isAnonymous: offerObj.isAnonymous,
        userId: offerObj.userId,
        userName: offerObj.userName,
        date: offerObj.noticeTime,
      });
      console.log(this.data)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})